<%@page import="model.environment.*"%>
<%@page import="java.text.*"%>
<%@page import="model.*"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<% 	PageViewInfo pvi = new PageViewInfo();
	pvi.setPageTitle("Administration - item sales");
	
	// Sending a 403 if the user isn't an admin.
	if (!Login.isAdministrator(session)) {
		response.sendError(403);
	}
%>
<%@include file="../parts/header.jsp" %>
<%
	Map<String, String> getData = StaticInfo.getGetData(request);
	
	String fromStr = getData.get("from");
	String toStr = getData.get("to");
	
	long from = 0;
	long to = 0;
	boolean filterDate = true;
	DateFormat formatter = new SimpleDateFormat("dd/MM/yy");
	try {
		
		Date fromDate = (Date) formatter.parse(fromStr);
		Date toDate = (Date) formatter.parse(toStr);
		from = fromDate.getTime();
		to = toDate.getTime();
	}
	catch (Exception e) {
		filterDate = false;
		// This could be just about anything, nullpointer etc. I dont care!.
	}
	DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.ENGLISH);
	%>

	<script>
	$(function() {
		$("#from").datepicker({dateFormat: "dd/mm/yy"});
		$("#to").datepicker({dateFormat: "dd/mm/yy"});
	});
	</script>
	<div class="content">
		<h1>All item sales</h1>
		<form action="<%= request.getRequestURL() %>" style="margin-bottom:30px;">
			<p>From: <input type="text" id="from" name="from" size="10" value="<%= getData.get("from") == null ? "" : getData.get("from") %>"/> 
			To: <input type="text" id="to" name="to" size="10"  value="<%= getData.get("to") == null ? "" : getData.get("to") %>"/>
			<input type="submit" value="Filter" style="font-size:17px;paddding:4px 7px;" /></p>
		</form>
		
		
		<div style="width:700px;border-top:1px solid black;float:left;" class="container">
			<div style="width:99px;border-right:1px dashed black;float:left;" class="coll">
				<h3 style="text-align:left;vertical-align:middle;">Name</h3>
			</div>
			<div style="width:129px;border-right:1px dashed black;float:left;" class="coll">
				<h3 style="text-align:left;vertical-align:middle;">Item</h3>
			</div>
			<div style="width:69px;border-right:1px dashed black;float:left;" class="coll">
				<h3 style="text-align:left;vertical-align:middle;">pcs.</h3>
			</div>
			<div style="width:149px;border-right:1px dashed black;float:left;" class="coll">
				<h3 style="text-align:left;vertical-align:middle;">$ per pc.</h3>
			</div>
			<div style="width:99px;border-right:1px dashed black;float:left;" class="coll">
				<h3 style="text-align:left;vertical-align:middle;">Total $</h3>
			</div>
			<div style="width:150px;float:left;" class="coll">
				<h3 style="text-align:left;vertical-align:middle;">Date</h3>
			</div>
		</div>
		
		<%
		List<Sale> itemSales = Sales.getShopSales();
		int sumPrice = 0;
		int sumPcs = 0;
		int sum = 0;
	 	int averagePrice = 0;
	 	List<Sale> localItemSalesList = new ArrayList<Sale>();
	 	localItemSalesList.addAll(itemSales);
		Collections.reverse(localItemSalesList); // Sort by date decreasing order. 
		for (Sale s : localItemSalesList) {
			if (filterDate)
			{
				if (s.getSaleTime() < from)
				{
					continue;
				}
				if (s.getSaleTime() > to)
				{
					continue;
				}
			}
			averagePrice += s.getSaleItemPrice();
			sumPrice += s.getSaleItemPrice() * s.getSaleAmount();
			sumPcs += s.getSaleAmount();
			sum++;
			%>
	
		<div style="width:700px;border-top:1px solid black;float:left;" class="container">
			<div style="width:99px;border-right:1px dashed black;float:left;" class="coll">
				<h3 style="text-align:left;vertical-align:middle;"><a href='<%= request.getContextPath() %>/admin/showCustomerSales.jsp?customerID=<%= s.getCustomerID() %>'><%= Customers.getCustomer(s.getCustomerID()).getCustomerName() %></a> </h3>
			</div>
			<div style="width:129px;border-right:1px dashed black;float:left;" class="coll">
				<h3 style="text-align:left;vertical-align:middle;"><a href="<%= request.getContextPath() %>/admin/showItemSales.jsp?itemID=<%= s.getItemID() %>"><%= Items.getItem(s.getItemID()).getItemName() %></a></h3>
			</div>
			<div style="width:69px;border-right:1px dashed black;float:left;" class="coll">
				<h3 style="text-align:left;vertical-align:middle;"><%= s.getSaleAmount() %></h3>
			</div>
			<div style="width:149px;border-right:1px dashed black;float:left;" class="coll">
				<h3 style="text-align:left;vertical-align:middle;">$<%= s.getSaleItemPrice() %></h3>
			</div>
			<div style="width:99px;border-right:1px dashed black;float:left;" class="coll">
				<h3 style="text-align:left;vertical-align:middle;">$<%= s.getSaleItemPrice() * s.getSaleAmount() %></h3>
			</div>
			<div style="width:150px;float:left;" class="coll">
				<h3 style="text-align:left;vertical-align:middle;"><%= df.format(new Date(s.getSaleTime())) %></h3>
			</div>
		</div>
		<%}%>
		<% if (sum != 0)
			averagePrice /= sum; %>
		<div style="width:700px;border-top:1px solid black;float:left;" class="container">
			<div style="width:99px;border-right:1px dashed black;float:left;" class="coll">
				<h3 style="text-align:left;vertical-align:middle;">
					A total of:
				</h3>
			</div>
			<div style="width:129px;border-right:1px dashed black;float:left;" class="coll">
				<h3 style="text-align:left;vertical-align:middle;">
					<%=sum %> sales
				</h3>
			</div>
			<div style="width:69px;border-right:1px dashed black;float:left;" class="coll">
				<h3 style="text-align:left;vertical-align:middle;">
					<%= sumPcs %> pcs.
				</h3>
			</div>
			<div style="width:149px;border-right:1px dashed black;float:left;" class="coll">
				<h3 style="text-align:left;vertical-align:middle;"><!-- $<%= averagePrice %> --> - </h3>
			</div>
			<div style="width:149px;border-right:1px dashed black;float:left;" class="coll">
				<h3 style="text-align:left;vertical-align:middle;">$<%= sumPrice %></h3>
			</div>
		</div>
		
		
 	</div>
<%@include file="../parts/footer.jsp" %>